import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtVolunteerApplyApi } from '#/api/cmt/person/volunteerApply';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getRangePickerDefaultProps } from '#/utils';

/** 申请类型选项 */
export const APPLY_TYPE_OPTIONS = [
  { label: '志愿者', value: 1 },
  { label: '师资', value: 2 },
];

/** 性别选项 */
export const GENDER_OPTIONS = [
  { label: '男', value: 1 },
  { label: '女', value: 2 },
];

/** 审核状态选项 */
export const AUDIT_STATUS_OPTIONS = [
  { label: '待审核', value: 0 },
  { label: '已通过', value: 1 },
  { label: '已拒绝', value: 2 },
];

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      formItemClass: 'col-span-2',
      componentProps: {
        api: () => getBaseCommunityOptions(),
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择社区',
      },
    },
    {
      fieldName: 'applyType',
      label: '申请类型',
      rules: 'required',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: APPLY_TYPE_OPTIONS,
      },
      defaultValue: 1,
    },
    {
      fieldName: 'realName',
      label: '真实姓名',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入真实姓名',
      },
    },
    {
      fieldName: 'gender',
      label: '性别',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: GENDER_OPTIONS,
      },
      defaultValue: 1,
    },
    {
      fieldName: 'age',
      label: '年龄',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入年龄',
        min: 1,
        max: 150,
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'phone',
      label: '联系电话',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入联系电话',
      },
    },
    {
      fieldName: 'idCard',
      label: '身份证号',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入身份证号',
      },
    },
    {
      fieldName: 'education',
      label: '学历',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入学历',
      },
    },
    {
      fieldName: 'occupation',
      label: '职业',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入职业',
      },
    },
    {
      fieldName: 'skills',
      label: '特长技能',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入特长技能，多个技能用逗号分隔',
      },
    },
    {
      fieldName: 'idCardFront',
      label: '身份证正面',
      component: 'ImageUpload',
      formItemClass: 'col-span-1',
    },
    {
      fieldName: 'idCardBack',
      label: '身份证反面',
      component: 'ImageUpload',
      formItemClass: 'col-span-1',
    },
    {
      fieldName: 'certificate',
      label: '资格证书',
      component: 'ImageUpload',
      formItemClass: 'col-span-2',
      componentProps: {
        maxCount: 5,
      },
    },
    {
      fieldName: 'applyReason',
      label: '申请理由',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 4,
        placeholder: '请输入申请理由',
      },
    },
    {
      fieldName: 'availableTime',
      label: '可服务时间',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '例如：周末全天、工作日晚上',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: AUDIT_STATUS_OPTIONS,
      },
      defaultValue: 0,
    },
    {
      fieldName: 'auditRemark',
      label: '审核备注',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入审核备注',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      componentProps: {
        api: () => getBaseCommunityOptions(),
        labelField: 'name',
        valueField: 'id',
        clearable: true,
        placeholder: '请选择社区',
      },
    },
    {
      fieldName: 'applyType',
      label: '申请类型',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: APPLY_TYPE_OPTIONS,
        placeholder: '请选择申请类型',
      },
    },
    {
      fieldName: 'realName',
      label: '真实姓名',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入真实姓名',
      },
    },
    {
      fieldName: 'phone',
      label: '联系电话',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入联系电话',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: AUDIT_STATUS_OPTIONS,
        placeholder: '请选择审核状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '申请时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtVolunteerApplyApi.VolunteerApply>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '申请ID',
      minWidth: 120,
    },
    {
      field: 'applyType',
      title: '申请类型',
      minWidth: 100,
      slots: { default: 'applyType' },
    },
    {
      field: 'realName',
      title: '真实姓名',
      minWidth: 120,
    },
    {
      field: 'gender',
      title: '性别',
      minWidth: 80,
      slots: { default: 'gender' },
    },
    {
      field: 'age',
      title: '年龄',
      minWidth: 80,
    },
    {
      field: 'phone',
      title: '联系电话',
      minWidth: 130,
    },
    {
      field: 'education',
      title: '学历',
      minWidth: 100,
    },
    {
      field: 'occupation',
      title: '职业',
      minWidth: 120,
    },
    {
      field: 'auditStatus',
      title: '审核状态',
      minWidth: 100,
      slots: { default: 'auditStatus' },
    },
    {
      field: 'auditTime',
      title: '审核时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'createTime',
      title: '申请时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
